package com.wnhz.work.yz.fragment.project;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.JobauditActivity;
import com.wnhz.work.yz.adapter.JobItemAdapter;
import com.wnhz.work.yz.base.BaseFragment;
import com.wnhz.work.yz.bean.JobProjectBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class JobApproveFragment extends BaseFragment {

    private View view;
    private JobauditActivity activity;
    private String type;
    private ListView my_listView;
    private Context context;

    @Override
    public void onResume() {
        upload("1");
        super.onResume();
    }
    public void setContext(Context context) {
        this.context = context;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_job_approve, container, false);
        activity = (JobauditActivity) getActivity();
        initView(view);
        upload("1");
        return view;
    }


    private void initView(View view) {
        my_listView = (ListView) view.findViewById(R.id.my_listView);

    }

    private void initData() {
        if (meetingData == null) {
            return;
        }
        List<JobProjectBean.InfoBean> info = meetingData.getInfo();
        // Toast.makeText(context, "JobAllFragment:" + jobBeanList.size(), Toast.LENGTH_SHORT).show();
        my_listView.setAdapter(new JobItemAdapter(context, info));
    }


    public void setData(List<JobProjectBean> jobBeanList) {

    }

    JobProjectBean meetingData;

    @Override
    public void onDestroy() {
        if (handler != null)
            handler.removeCallbacks(null);
        super.onDestroy();
    }

    public void upload(String type) {
        //  showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("type", type);
        XUtil.Post(Url.ORDER_GETOSHENPINGTEPI, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("2-------", result);
//                Log.i(TAG, "onSuccess: AllOrderActivity= " +result.substring(0, result.length() / 2));
//                Log.i(TAG, "onSuccess: AllOrderActivity= " + result.substring(result.length() / 2, (result.length() / 2 * 1)));
//                Log.i(TAG, "onSuccess: AllOrderActivity= " + result.substring((result.length() / 2 * 1), result.length()));
                //  JSONObject jsonObject = new JSONObject(result);
                //  String res = jsonObject.optString("result");
                // JSONArray jsonArray = jsonObject.optJSONArray("info");

                Gson gson = new Gson();
                meetingData = gson.fromJson(result, JobProjectBean.class);

            }

            @Override
            public void onFinished() {
                //    closeDialog();
                //    initData();
                handler.sendEmptyMessage(1);
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                //  closeDialog();
                Log.e("-----", "tyuyuty");
                super.onError(ex, isOnCallback);
                //  Log.e(TAG, "onError: " + ex);
            }
        });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            initData();
        }
    };
}

